package com.college.controllers;

import java.util.List;

import javax.jms.Session;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.servlet.ModelAndView;

import com.college.domain.Batch;
import com.college.domain.ChangePassword;
import com.college.domain.College;
import com.college.domain.Department;
import com.college.domain.MapSemesterForm;
import com.college.domain.Semester;
import com.college.dto.MapSemesterDTO;
import com.college.interfaces.IConfigurationDAO;

@Controller("/config")
public class ConfigurationController {
	
	@Autowired
	private IConfigurationDAO configDAO;
	

	@RequestMapping(value="/addCollege", method=RequestMethod.GET)
	public ModelAndView getCollegeForm()
	{
		ModelAndView mav = new ModelAndView("add_college");
		College college = new College();
		mav.getModelMap().put("add_college", college);
		return mav;
	}
	@RequestMapping(value="/addCollege", method=RequestMethod.POST)
	public String saveCollege(@ModelAttribute("add_college")@Valid College college, BindingResult result, SessionStatus status, ModelMap model){
		if(result.hasErrors()){
			return "add_college";
		}
		Integer res = configDAO.saveCollege(college);
		if(res!=null){
			status.setComplete();
			model.addAttribute("savedMsg", "College is added Successfully.");
			
		}
		return "add_college";
		
	}
	
	@RequestMapping(value="/addDepartment", method=RequestMethod.GET)
	public ModelAndView getDeptForm(ModelMap model)
	{
		ModelAndView mav = new ModelAndView("add_dept");
		List<String> collegeList = configDAO.getCollegeListForDropDown();
		model.addAttribute("collegelist",collegeList);
		Department dept = new Department();
		mav.getModelMap().put("add_dept", dept);
		return mav;
	}
	@RequestMapping(value="/addDepartment", method=RequestMethod.POST)
	public String saveDepartment(@ModelAttribute("add_dept")@Valid Department dept, BindingResult result, SessionStatus status, ModelMap model){
		if(result.hasErrors()){
			return "add_dept";
		}
		Integer res = configDAO.saveDept(dept);
		if(res!=null){
			status.setComplete();
			model.addAttribute("savedMsg", "Department is added Successfully.");
			
		}
		return "add_dept";
		
	}
	
	@RequestMapping(value="/addBatch", method=RequestMethod.GET)
	public ModelAndView getBatchForm(ModelMap model)
	{
		ModelAndView mav = new ModelAndView("add_batch");
		List<String> deptList = configDAO.getDeptListForDropDown();
		model.addAttribute("deptlist",deptList);
		Batch batch = new Batch();
		mav.getModelMap().put("add_batch", batch);
		return mav;
	}
	@RequestMapping(value="/addBatch", method=RequestMethod.POST)
	public String saveDepartment(@ModelAttribute("add_batch")@Valid Batch batch, BindingResult result, SessionStatus status, ModelMap model){
		if(result.hasErrors()){
			return "add_batch";
		}
		Integer res = configDAO.saveBatch(batch);
		if(res!=null){
			status.setComplete();
			model.addAttribute("savedMsg", "Batch added Successfully.");
			
		}
		return "add_batch";
		
	}
	
	@RequestMapping(value="/changePassword", method=RequestMethod.GET)
	public ModelAndView getChangePasswordForm(ModelMap model)
	{
		ModelAndView mav = new ModelAndView("change_password");
		ChangePassword password = new ChangePassword();
		mav.getModelMap().put("change_password", password);
		return mav;
	}
	
	@RequestMapping(value="/changePassword", method=RequestMethod.POST)
	public String changePassword(@ModelAttribute("change_password")ChangePassword password, BindingResult result,HttpSession session, SessionStatus status, ModelMap model){
		if(result.hasErrors()){
			return "change_password";
		}
		String userName = (String) session.getAttribute("userId");
		int res = configDAO.updateUserPassword(password, userName);
		if(res==1){
			status.setComplete();
			model.addAttribute("savedMsg", "Password Changed Successfully.");
			
		}
		else{
			model.addAttribute("savedMsg", "Password and Confirm password does not match.");
		}
		return "change_password";
		
	}
	
	@RequestMapping(value="/mapSemester", method=RequestMethod.GET)
	public ModelAndView getSemesterForm(ModelMap model)
	{
		ModelAndView mav = new ModelAndView("add_semester");
		Semester sem = new Semester();
		List<MapSemesterDTO> mapSemDTO = configDAO.getMappedBatchSem();
		MapSemesterForm mapSemesterForm = new MapSemesterForm();
		mapSemesterForm.setMapSemDTO(mapSemDTO);
		model.addAttribute("mapSemesterForm",mapSemesterForm);
		mav.getModelMap().put("add_semester", sem);
	
		return mav;
	}
}